layui.use(['form', 'element', 'util', 'carousel', 'form', 'laypage', 'layer','table','upload'], function () {
    var table = layui.table;
    var form = layui.form;
    table.render({
        elem: '#order'
        , url: basePath + '/user/order/formorder'
        , page: {
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip']
            , groups: 3
            , limits: [20, 50, 100]
            , limit: 20
            , totalRow: true    //开启合计行
        }, cols: [[
            {field: 'commid', title: '商品编号', width: 200, align: 'center'}
            , {field: 'commname', title: '名称', width: 150, align: 'center'}
            , {field: 'commdesc', title: '描述', width: 250, align: 'center'}
            , {field: 'thinkmoney', title: '价格（RMB）', width: 150, align: 'center', sort: true, totalRow: true}
            , {field: 'category', title: '农场', width: 150, align: 'center'}
            , {field: 'number', title: '数量', width: 80, align: 'center', sort: true, totalRow: true}
        ]]
        , width: 980
        , height: 500
        , done: function (res, curr, count) {
            var TotalSum = 0;
            for (var i = 0; i < res.data.length; i++) {
                TotalSum += res.data[i].thinkmoney * res.data[i].number;//总金额
            }
            $("#total_price").val(TotalSum.toFixed(2));
        }
    });

    form.on('submit(demo2)', function(data){
        var object = new Object();
        object["receivename"] = data.field.receivename;
        object["receivephone"] = data.field.receivephone;
        object["receiveaddress"] = data.field.receiveaddress;
        var jsonData = JSON.stringify(object);
        $.ajax({
            url: basePath + "/order/pay-success",
            data: jsonData,
            contentType: "application/json;charset=UTF-8",
            type: "post",
            dataType: "json",
            beforeSend: function () {
                layer.load(1, { //icon支持传入0-2
                    content: '支付中...',
                    success: function (layero) {
                        layero.find('.layui-layer-content').css({
                            'padding-top': '39px',
                            'width': '60px'
                        });
                    }
                });
            },
            complete: function () {
                layer.closeAll('loading');
            },
            success: function (data) {
                if (data.status == 200) {
                    layer.msg(data.message, {
                        time: 2000,
                        icon: 16,
                        offset: '100px'
                    }, function () {
                        location.reload();
                    });
                    setTimeout( function(){
                        window.close();
                        window.open(basePath + "/order-detail/" + data.data);
                        window.focus();
                    }, 2 * 1000 );//延迟2000毫秒
                } else {
                    layer.msg(data.message, {
                        time: 1000,
                        icon: 2,
                        offset: '100px'
                    });
                }

            },error:function () {
                layer.msg('系统异常');
            }
        });
        return false;
    });
});